<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>展示轮播图</title>
    <link rel="stylesheet" href="/js/bootstrap.min.css">
    <script src="/js/jquery-3.2.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var carousel = $("#carousel");
            var pageNo = 1;
            var total = null;


            //设置首页
            $("#first").click(function () {
                pageNo = 1;
                show();
            });

            //设置上一页
            $("#prev").click(function () {
                if (pageNo <= 1)
                    return;
                pageNo--;
                show();
            });

            //设置下一页
            $("#next").click(function () {
                if (pageNo >= total)
                    return;
                pageNo++;
                show();
            });

            //设置尾页
            $("#last").click(function () {
                pageNo = total;
                show();
            });

            var title = null;
            $("#button").click(function () {
                title = $("#title").val();
                pageNo = 1;
                show();
                return false;
            });
            show();

            function show() {
                carousel.find("tr:gt(0)").remove();
                $.get("/applets/carousel/page", {"pageNo": pageNo, "pageSize": 5, "title": title}, function (da) {
                    if (da.code != 0) {
                        alert(da.msg);
                        return;
                    }
                    var map = da.data;
                    total = map.total;
                    $.each(map.list, function (i, d) {
                        carousel.append("<tr><td>" + d.id +
                            "</td><td>" + d.url +
                            "</td><td>" + getRes(d.inuse) +
                            "</td><td>" + d.title +
                            "</td><td>" + d.linkurl +
                            "</td><td>" + d.modifydate +
                            "</td><td><a class='btn btn-primary btn-sm' href='/applets/carousel/select?id=" + d.id + "'>更新</a>&nbsp;" +
                            "<button type='button' class='btn btn-primary btn-sm' code='" + d.id + "' id='shanchu'>删除</button></td></tr>")
                    })
                });
            };
            var ids = null;
            //删除事件
            carousel.on("click", '#shanchu', function () {
                ids = $(this).attr("code");
                del();
                return false;
            });

            //ajax删除
            function del() {
                var a = confirm("是否删除该轮播图?");
                if (!a)
                    return;
                $.ajax({
                    url: "/applets/carousel/detele",
                    data: {"carouselId": ids},
                    type: "post",
                    success: function (ur) {
                        alert(ur.msg);
                    }
                });
            };

            function getRes(q) {
                if (q == true)
                    return "是";
                return "否"
            }
        });
    </script>
</head>
<body>

<div class="container">
    <h2 class="text-center">展示轮播图</h2>

    <!--导航-->
    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" href="/Menu.html">返回主菜单</a>
        </li>
        <li class="nav-item">
            <a class="nav-link " href="AddCarousel.html">添加轮播图</a>
        </li>
    </ul>

    <form action="#" method="post">
        <div class="form-group">
            <label>图片标题(模糊)</label>
            <input type="text" name="title" class="form-control" id="title">
        </div>
        <div class="form-group">
            <input type="button" id="button" class="form-control" value="查找轮播图">
        </div>
    </form>
</div>

<div class="container">
    <table id="carousel" class="table table-striped">
        <thead>
        <tr>
            <th>ID</th>
            <th>图片地址</th>
            <th>是否使用</th>
            <th>图片标题</th>
            <th>链接地址</th>
            <th>修改时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

<div class="container offset-md-5">
    <ul class="pagination">
        <li class="page-item" id="first"><a class="page-link">首页</a></li>
        <li class="page-item" id="prev"><a class="page-link">上一页</a></li>
        <li class="page-item" id="next"><a class="page-link">下一页</a></li>
        <li class="page-item" id="last"><a class="page-link">尾页</a></li>
    </ul>
</div>
</body>
</html>